  %se imprimen el promedio y la varianza y estan los 2 graficos
   
  
  n=1000;
  tol=0.001;
  
  % r 30 numeros random de 1 a 36 ???ra calcular la prob
  r=zeros(1,30);
  %cot cambio de cotizacion diario
  cot=zeros(1,30);
  %val val(i)=val(i-1)*(1-cot(i)) --> valor del dia es el valor del dia anterior con el cambio en la cotizacion de hoy
  val=zeros(1,30);
  %ben ben(i)=val(i)-val(0)
  ben=zeros(1,30);
  %men 5000 beneficios mensuales distintos
  men=zeros(1,n);
  
  %a_0 cant inicial
  a_0=0;
  
  promedio=zeros(1,n);
  vars=zeros(1,n);
  varianza=zeros(1,n);
  
last = a_0;
r=rand(1,n)*36 + 1;
	  
 cutValue=n;
    for i=1:n
        
          if ( r(i)<4 )
        cot(i)=-1/8;
          end
          if ( r(i)>=4 && r(i)<11 )
        cot(i)=0;
          end
          if ( r(i)>=11 && r(i)<27 )
        cot(i)=1/8;
          end
          if ( r(i)>=27 && r(i)<34 )
        cot(i)=1/2;
          end
          if ( r(i)>=34 )
        cot(i)=1;
          end

      ben(i)=cot(i);
          
          if(i<=1)
            promedio(1)=ben(i);
            vars(1)=0;
            varianza(i)=0;
          else
            promedio(i) = promedio(i-1) + (ben(i) - promedio(i-1))/i;
            vars(i) = vars(i-1) + (ben(i) - promedio(i-1))*( ben(i) - promedio(i));
            %varianza(i)= sqrt( vars(i) / (i-1)); % = standard deviation ...
            varianza(i) = vars(i) / (i-1);
          end

% condici??n de corte, break condition 
%           if(i>1)  
%               if(abs(vars(i)-vars(i-1))<tol)
%                 cutValue=i
%                 break;
%               end
%           end

    end
%    cot;
    
%Mean day2day benefit
figure(10);
clf;

plot(1:cutValue, promedio(1:cutValue), '-', 'Color', cDarkGrey);

xlbl = xlabel('\it Numero de realizaciones n');
ylbl = ylabel('\it Beneficio medio dia a dia ');
set(xlbl, 'Interpreter', 'tex', 'FontName', 'Times', 'FontSize', iStdSize);
set(ylbl, 'Interpreter', 'tex', 'FontName', 'Times', 'FontSize', iStdSize);

%Variance of day2day benefit
figure(11);
clf;

plot(1:cutValue,varianza(1:cutValue),'-','Color',cDarkGrey);

xlbl = xlabel('\it Numero de realizaciones n');
ylbl = ylabel('\it Varianza del beneficio dia a dia');
set(xlbl, 'Interpreter', 'tex', 'FontName', 'Times', 'FontSize', iStdSize);
set(ylbl, 'Interpreter', 'tex', 'FontName', 'Times', 'FontSize', iStdSize);


% print the result of the final mean and variance values on the screen ...
disp('result:');
fprintf('mean of the daily benefit: %f\n\n', promedio(n));
fprintf('variance of the daily benefit: %f\n\n', varianza(n));


% result of "mc_variance_benefit_daily_v3.eps"
% result:
% mean of the daily benefit: 0.226750
% 
% variance of the daily benefit: 0.083449


% result of "mc_variance_benefit_daily_v4.eps
% and "mc_mean_benefit_daily_v4.eps:
%
% mean of the daily benefit: 0.227875
% 
% variance of the daily benefit: 0.093338
    
